<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-3-16
 * Time: 下午6:11
 */
namespace Admin\Model;
use Think\Model\RelationModel;

class FacModel extends RelationModel {

    public function getManufacturer(){
        //查询所有的厂家
        $manufacturer = M("fac")->where("pid = 0 and type = 2")->field("id,name,fac_name,fac_login_name")->select();
        foreach($manufacturer as $key =>$value){
            $device_type = M("device_type")->where("manufacturer_id =".$value['id'])->order("id")->select();
            foreach($device_type as $k =>$v){
                if($v['level'] == 1){
                    $manufacturer[$key]['area'][$v['id']] = $v;
                    $manufacturer[$key]['area'][$v['id']]['area'] = array();
                }
                if($v['level'] == 2){
                    $manufacturer[$key]['area'][$v['pid']]['area'][$v['id']] = $v;
                }
            }
        }
        return $manufacturer;
    }
    //以厂商编号为查询条件
    public function getManufacNew($map){
        //查询指定的厂家
        $manufacturer = M("fac")->where("fac_login_name='".$map."' and pid = 0 and type = 2")->field("id,name,fac_name")->select();
        foreach($manufacturer as $key =>$value){
            $device_type = M("device_type")->where("manufacturer_id =".$value['id'])->order("id")->select();
            foreach($device_type as $k =>$v){
                if($v['level'] == 1){
                    $manufacturer[$key]['area'][$v['id']] = $v;
                    $manufacturer[$key]['area'][$v['id']]['area'] = array();
                }
                if($v['level'] == 2){
                    $manufacturer[$key]['area'][$v['pid']]['area'][$v['id']] = $v;
                }
            }
        }
        return $manufacturer;
    }
    //以id为查询条件
    public function getManufacNews($map){
        //查询指定的厂家
        $manufacturer = M("fac")->where("id='".$map."'")->field("id,name,fac_name")->select();
        foreach($manufacturer as $key =>$value){
            $device_type = M("device_type")->where("manufacturer_id =".$value['id'])->order("id")->select();
            foreach($device_type as $k =>$v){
                if($v['level'] == 1){
                    $manufacturer[$key]['area'][$v['id']] = $v;
                    $manufacturer[$key]['area'][$v['id']]['area'] = array();
                }
                if($v['level'] == 2){
                    $manufacturer[$key]['area'][$v['pid']]['area'][$v['id']] = $v;
                }
            }
        }
        return $manufacturer;
    }
}